package com.woniu.day8.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.hutool.crypto.digest.DigestUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.day8.mapper.UserInfoMapper;
import com.woniu.day8.pojo.BuildingPo;
import com.woniu.day8.pojo.Menu;
import com.woniu.day8.pojo.UserInfo;
import com.woniu.day8.pojo.dto.UserInfoDto;
import com.woniu.day8.service.MenuService;
import com.woniu.day8.service.UserInfoService;
import com.woniu.day8.util.ResultMessage;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import static com.woniu.day8.util.FinalConst.SUCCESS;

@RestController
@RequestMapping("/userinfo")
@CrossOrigin
@SaCheckLogin
public class  UserController {
    @Autowired
    private MenuService menuService;
    @Autowired
    private UserInfoService userInfoService;
    /***
     * 菜单查询
     * @param id
     * @return
     */
    @RequestMapping("/selectmenu")
    public ResultMessage selectmenu(@RequestParam Integer id) {
        List<Menu> list = menuService.SelectMenu(id);
            return ResultMessage.success(list);
    }
    /***
     * 用户角色查询
     * @param udto
     */
    @RequestMapping("/selectUser")
    public ResultMessage selectUser(@RequestBody UserInfoDto udto){
        Long pageNum = udto.getPageNum(); // 拿到页码
        Long pageSize = udto.getPageSize(); // 拿到页大小
        UserInfo user = new UserInfo();
        BeanUtils.copyProperties(udto,user);
        Page<UserInfo> page = new Page<>(pageNum, pageSize);

        // 调用新的多条件查询方法
        List<UserInfo> list = userInfoService.selectUserByConditions(page, user);

        Map<String, Object> result = new HashMap<>();
        result.put("records", list); // 查询数据
        result.put("total", page.getTotal()); // 数据总条数
        result.put("current", page.getCurrent()); // 当前页
        result.put("pages", page.getPages()); // 总页数
        result.put("size", page.getSize()); // 页大小

        if (list != null && !list.isEmpty()){
            return ResultMessage.success(result);
        }
        return ResultMessage.fail("获取失败");
    }
    /**
     * 文件上传
     * @param file
     */
    @PostMapping("/uploadFile")
    public ResultMessage uploadFile(MultipartFile file) throws IOException {
        //指定文件上传路径
        String basePath = "E:\\java\\static\\img";
        //指定访问上传文件的路径
        String baseUrl = "http://localhost:80/";
        String originalFileName = file.getOriginalFilename();
        int startIndex =originalFileName.lastIndexOf(".");
        String suffix = originalFileName.substring(startIndex);
        String prefix = UUID.randomUUID().toString().replace("-","").toUpperCase();
        String newname = prefix+suffix;
        file.transferTo(new File(basePath,newname));
        String url = baseUrl+newname;
        return ResultMessage.success("success",SUCCESS,url );
    }
    /**
     * 新增用户
     * @param Duser
     */
    @RequestMapping("/insertUser")
    public ResultMessage insertUser(@RequestBody UserInfoDto Duser){
        // 检查用户名是否已存在
        QueryWrapper<UserInfo> checkWrapper = new QueryWrapper<>();
        checkWrapper.eq("username", Duser.getUsername());
        UserInfo existingUser = userInfoService.getOne(checkWrapper);
        if (existingUser != null) {
            return ResultMessage.fail("用户名已存在");
        }

        //处理前段数据
        Duser.setIsdelete(0);
        //MD5加密密码
        String md5Pwd = DigestUtils.md5DigestAsHex("123".getBytes());
        Duser.setPassword(md5Pwd);
        UserInfo user = new UserInfo();
        BeanUtils.copyProperties(Duser,user);
        userInfoService.save(user);
        return ResultMessage.success("添加成功");
    }
    /**
     * 修改用户
     * @param Duser
     */
    @RequestMapping("/updata")
    public  ResultMessage updata(@RequestBody UserInfoDto Duser){
        UserInfo user = new UserInfo();
        BeanUtils.copyProperties(Duser,user);
        if(userInfoService.updateById(user)){
            return ResultMessage.success("修改成功");
        }
        return   ResultMessage.fail("修改失败");
    }
    /**
     * 删除用户
     * @param Duser
     */
    @RequestMapping("/delete")
    public  ResultMessage delete(@RequestBody UserInfoDto Duser){
        UserInfo user = new UserInfo();
        BeanUtils.copyProperties(Duser,user);
        if(userInfoService.removeById(user)){
            return ResultMessage.success("删除成功");
        }
        return   ResultMessage.fail("修删除失败");
    }

}
